package com.qdlc.p2p.biz.service.activity.plan;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.NewActivityPlanService;
import com.qdlc.p2p.biz.service.NewActivityRuleService;
import com.qdlc.p2p.biz.service.activity.AbstractActivityPlanHandler;
import com.qdlc.p2p.biz.service.activity.ActivityPlanEnum;
import com.qdlc.p2p.biz.service.activity.ActivityPlanException;
import com.qdlc.p2p.biz.service.activity.ActivityPlanParam;
import com.qdlc.p2p.biz.service.activity.ActivityProgrammeEnum;
import com.qdlc.p2p.biz.service.activity.ActivityProgrammeHandler;
import com.qdlc.p2p.biz.service.activity.ActivityProgrammeParam;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dto.NewActivityPlan;
import com.qdlc.p2p.dal.dto.NewActivityRule;

/**
 * 活动计划环节处理器
 * @author linya
 */
@Service
public class DefaultActivityPlanHandler extends AbstractActivityPlanHandler{

	@Resource
	NewActivityPlanService newActivityPlanService;
    
	@Resource
	NewActivityRuleService newActivityRuleService;
    
	/**
	 * 执行活动
	 * @param planEnum
	 * @param param
	 * @throws ActivityPlanException 
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void doPlan(ActivityPlanParam planParam) throws ActivityPlanException {
		
		//获取活动计划类型
		ActivityPlanEnum planEnum = planParam.getActivityPlan();
		if(planEnum == null){
			throw new ActivityPlanException("++++ please set a ActivityPlanEnum for ActivityPlanParam");
		}
		
		//获取活动计划环节
		NewActivityPlan activityPlan = newActivityPlanService.getActivityPlanByTache(planEnum.getEnTache());
		
		//检测活动是否可行
		boolean activityEnabled = checkActivity(activityPlan);
		if(!activityEnabled){
			return;
		}
		
		//查找该活动计划环节中活动方案
		List<NewActivityRule> activityProgrammeList = newActivityRuleService.findByActivityId(activityPlan.getId());
		for(NewActivityRule activityProgramme: activityProgrammeList){
			//活动方案类型
			int type = activityProgramme.getType();
			ActivityProgrammeEnum programmeEnum = ActivityProgrammeEnum.getProgrammeEnum(type);
			
			//获取方案处理器
			ActivityProgrammeHandler handler = activityProgrammeMap.get(programmeEnum);
			
			//设置方案基本参数
			ActivityProgrammeParam param = planParam.getProgrammeParam(programmeEnum);
			ActivityProgrammeParam programmeParam = handler.createProgrammeParam(param);
			programmeParam.setActivityPlan(planEnum);
			programmeParam.setRuleId(activityProgramme.getRuleId());
			programmeParam.setRuleType(type);
			programmeParam.setUser(planParam.getUser());
			
			try {
				handler.handle(activityProgramme, programmeParam);
			} catch (ActivityPlanException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 检测活动是否可行
	 * @param newActivityPlan
	 * @return
	 */
	private boolean checkActivity(NewActivityPlan newActivityPlan) {
        
		if (newActivityPlan == null) {
            return false;
        }
        
		Date start_time = newActivityPlan.getStartTime();
        Date end_time = newActivityPlan.getEndTime();
        int status = newActivityPlan.getStatus();
        
        // 0:开启，1:关闭
        if (status != 0) {
            return false;
        }
        
        Date date = DateUtil.getNow();
        //开始结束时间判定
        if (start_time.getTime() > date.getTime()
                || end_time.getTime() < date.getTime()) {
            return false;
        }
        
        return true;
    }
	
}
